package utils

import (
	"crypto/md5"
	"encoding/hex"
	"regexp"
	"strconv"

	"lottery/constant"
)

func Int(str string) int {
	i, _ := strconv.Atoi(str)
	return i
}

func Int64(str string) int64 {
	i, _ := strconv.ParseInt(str, 10, 64)
	return i
}

func Float64(str string) float64 {
	f, _ := strconv.ParseFloat(str, 64)
	return f
}

func MatchArray(id string, ids []string) bool {
	if id == "" {
		return false
	}
	for i := 0; i < len(ids); i++ {
		if id == ids[i] {
			return true
		}
	}
	return false
}

func Md5(str string) string {
	h := md5.New()
	h.Write([]byte(str))
	return hex.EncodeToString(h.Sum(nil))
}

const (
	CMReg   = "^1(3[4-9]|4[7]|5[0-27-9]|7[08]|8[2-478])\\d{8}$"
	CUReg   = "^1(3[0-2]|4[5]|5[56]|7[0156]|8[56])\\d{8}$"
	CTReg   = "^1(3[3]|4[9]|53|7[037]|8[019])\\d{8}$"
	MailReg = "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?"
)

func GetPhomeNumb(numb string) (t int) {
	if match, _ := regexp.MatchString(CMReg, numb); match {
		return constant.ChinaMobile
	}

	if match, _ := regexp.MatchString(CUReg, numb); match {
		return constant.ChinaUnicom
	}

	if match, _ := regexp.MatchString(CTReg, numb); match {
		return constant.ChinaTelec
	}

	return constant.Unknow
}

func ValidMail(mail string) (match bool) {
	match, _ = regexp.MatchString(MailReg, mail)
	return
}
